package grpc

import (
	"context"
	"dogapm"
	"github.com/spf13/cast"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	pb "proto"
	"usrsvc/dao"
)

type UserServer struct {
	pb.UnimplementedUserServiceServer
}

func (u *UserServer) GetUsersInfo(ctx context.Context, user *pb.User) (*pb.User, error) {
	userInfo, ok := dao.UserDao.Get(ctx, user.Id)
	if !ok {
		//ctx, span := dogapm.Tracer.Start(ctx, "GetUserInfo")
		//defer span.End()
		err := status.Errorf(codes.NotFound, "user not exits %d", user.Id)
		dogapm.Logger.Error(ctx, "GetUserInfo", map[string]interface{}{
			"msg": "user not found",
			"uid": user.Id,
		}, err)
		return nil, err
	}
	return &pb.User{
		Name: cast.ToString(userInfo.Name),
		Id:   cast.ToInt64(userInfo.ID),
	}, nil
}
